package com.haople.sso.core.util;

public class ByteUtil {

	/**
	 * 将二进制转化为 16进制 并拼接字符串
	 * @param b
	 * @return
	 */
	  public static String bytes2HexString(byte[] b) {
	        StringBuffer ret = new StringBuffer();
	        for (int i = 0; i < b.length; i++) {
	            String hex = Integer.toHexString(b[ i ] & 0xFF);
	            if (hex.length() == 1) {
	                hex = '0' + hex;
	            }
	            ret.append(hex.toUpperCase());
	        }
	        return ret.toString();
	    }
	  
	   /**将16进制转换为二进制 并拼接字符串
	     * @param hexStr 
	     * @return 
	     */  
	 public static byte[] hexStr2Byte(String hexStr) {  
	        if (hexStr.length() < 1){
	        	return null;  
	        }
	        byte[] result = new byte[hexStr.length()/2];  
	        for (int i = 0;i< hexStr.length()/2; i++) {  
	              int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);  
	              int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);  
	              result[i] = (byte) (high * 16 + low);  
	           }  
	       return result;  
	    }
}
